<?php
/**
 * 视图控制器
 */
namespace app\manage\controller\goods;

use app\manage\controller\Base;
use app\common\model\MallCategory;
class Api extends Base
{
      /**
     * 商品列表接口
     * @return [type] [description]
     */
    public function goods(){
        $params = $this->request->param();
        $perpagenum = isset($params['limit'])?intval($params['limit']):15;
        $perpagenum = $perpagenum>100?100:$perpagenum;
        $page = isset($params['page'])?intval($params['page']):1;
        $page = $page<1?1:$page;

        unset($params['limit']);
        unset($params['page']);
        $where = [];
        if(isset($params['key'])){
            $where= parseWhere($params['key']);
        }
        $datas = model('MallGoods')->getPageDatas($where,$page,$perpagenum);
        $datas['code'] = 1;
        return json($datas);
    }

    public function getOne(){
        $params = $this->request->param();
        $id = isset($params['id'])?intval($params['id']):0;
        if(!$id)
            return json(['code'=>0,'msg'=>'参数错误']);;
        $data = model('MallGoods')->getOneDataById($id);
        $data['code'] = 1;
        return json(['code'=>1,'data'=>$data]);
    }

    /**
     * 状态接口
     * @return [type] [description]
     */
    public function status_goods(){
        $params = $this->request->param();
        $status = isset($params['status'])?$params['status']:null;
        $data = \app\common\query\MallGoods::status($status);
        return json(['code'=>1,'msg'=>'商品状态列表','data'=>$data]);
    }
    /**
     * 导出商品列表
     * @return [type] [description]
     */
    public function export_index(){
        $params = $this->request->param();
        $where = parseWhere($params);
        $query = new \app\common\query\MallGoods;
        $datas = $query->getDatas($where,['title','price','coin_id','unit','score','weight','total','city','is_tj']);

        $excelData = "商品名称\t商品价格\t货币类型\t单位\t业绩\t重量\t库存\t出货地\t是否推荐\t\n";
        foreach($datas as $value):
            $excelData .= $value['title']."\t";
            $excelData .= $value['price']."\t";
            $excelData .= $value['coin_name']."\t";
            $excelData .= $value['unit']."\t";
            $excelData .= $value['score']."\t";
            $excelData .= $value['weight']."\t";
            $excelData .= $value['total']."\t";
            $excelData .= $value['city']."\t";
            $excelData .= $value['is_tjs']."\t";
            $excelData .= "\n";
        endforeach;
        $excelData = iconv('utf-8','gb2312',$excelData);

        exportExcel(time().'.xls',$excelData);
    }

    public function uploads(){
        $file = request()->file('image');
        $info = $file->validate(['size'=>1567800,'ext'=>'bmp,jpg,jpge,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads');
        if($info){
            $file_image = '/uploads/'.$info->getSaveName();
            return json(['errno'=>0,'data'=>[$file_image]]);
        }else{
            return 1;
            // 上传失败获取错误信息
            return json($file->getError());
        }
    }

}